(function () {
    fetch("http://chst.vip:1234/api/getcategorytitle")
      .then((body) => body.json())
      .then((res) => {
           console.log(res);
        let html = "";
        let { result: data } = res;
        data.forEach((item,index) => {
            // console.log(typeof data);
          smallNav(item.titleId,index);
          html += `
          <li>${item.title}<span>▼</span></li>
          <ol class="y-1"></ol>
          `;
        });
        document.querySelector("#nav ul").innerHTML += html;
        console.log($("#nav ul li"));

        //点击开关
        $("#nav ul li").click(function () {
          // console.log($(this).next().siblings().filter('ol'));
        $(this).next().siblings().filter('ol').hide();
        $(this).next().slideToggle();
        });
        
      });
  
    function smallNav(titleID,indexId) {
      fetch("http://chst.vip:1234/api/getcategory?titleid=" + titleID)
        .then((body) => body.json())
        .then((res) => {
          console.log(res);
          let { result: data } = res;
          let html1 = "";
          data.forEach((item) => {
            html1 += `

                  <li categoryid="${item.categoryId}" title="${item.category}">${item.category}</li>
                  `;
          });
          // document.querySelector("#nav ul ol").innerHTML += html1;
          let arr=[...document.querySelectorAll('#nav ul ol')]
          arr[indexId].innerHTML=html1
          // console.log(arr[indexId]);
          
          // console.log($("#nav ul ol"));
          
        });


    }

//创建点击事件,获取到ul里面的ol里的li并点击
    $("#nav ul").on("click",".y-1 li",function(){
      console.log(this);
        let title = $(this).attr("title")
        console.log(title);
      // $(this).attr(categoryid)
      //获取的点击元素的categoryid属性值
    let categoryid= $(this).attr("categoryid");
    //；链接详情页地址，传入所需要的参数属性值
    location.href=`../pages/bijiage.html?category=${title}&categoryid=${categoryid}`
    });
    
  })();
  